#! /usr/bin/python
# -*- coding: cp1250 -*-
#import sys; sys.path.append(r'C:/xxx/yyy')

import glob, re, string, sys, os
import pepe.txt.bigtxt as bt
import time

def main(ifl, ofl):

    ## redirect stdo to file
    #sys.stdout = open(ofl, 'w')
    ## close and redirect stdo back to normal
    #sys.stdout=sys.__stdout__

    res = []
    ll = 0
    output = open(ofl, 'w')
    output.write('''FILE^ROWNO^BIZTIPNEV^TELJESITES^BIZONYLATSZAM^CEGNEV^KELTE^ESEDEKES^HIVATKOZAS^HIVATKOZAS2^MOZGASNEV^DEVIZA^ARFOLY^SZTORNO^FIZMOD^FTBRU^FTFIZ^DEVBRU^DEVFIZ^KESZITETTE^FEJMEGJ^SOR^CIKK^CIKKNEV^MENNYISEG^EAR^KEDVSZ^KEDVFT^SZUM^ATL_ERTEK^DEVEAR^DEVSUM^SORHIV1^SORHIV2^SORHIV3^SORMEGJ^BFEJAZON^BIZTIP\n''')
    allino = 0
    for inpfile in glob.glob(ifl):
        lino = 1
        fname = re.sub(r'01csv\\', '', inpfile)
        fname = re.sub('_XLS_EXPORT\.xls\.csv', '', fname)

        for line in bt.lines(inpfile):
            line = re.sub(r"01csv\\", '', line[4:])
            line = re.sub('(?P<x>\d{4}\.\d{2}\.\d{2})\.','\g<x>', line) # FIX "YYYY.MM.DD." PROBLEM
            if lino == 1:
                pass
            else:
                output.write("%s^%s^%s"%(fname, lino, line))
            lino += 1
            if line <> '':
                ll = len(string.split(line, '^'))
                if ll not in res: res.append(ll)
                allino += 1

    output.close()
    print str(allino), str(ll)

if __name__ == "__main__":
    main('01csv/*.csv','01.txt')
